local M = { }

M.sync_server = "time.nist.gov"

function M.synctime(ip)
    sntp.sync(ip, nil, nil)
end

function M.format(year, month, date, hour, minite, secend)
    return string.format("%04d-%02d-%02d %02d:%02d:%02d",year, month, date, hour, minite, secend)
end

function M.analyse(time, zone)

    time = time + zone * 3600

    local year = 1970
    local month = 1
    local date = 1
    local hour = 0
    local minite = 0
    local secend = 0

    secend = secend + time % 60;
    time = time / 60;

    minite = minite + time % 60;
    time = time / 60;

    hour = hour + time % 24;
    time = time / 24;


    year = time / 1461 * 4 + year;
    date = time % 1461;


    local mon = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }

    if 365 < date and date <= 730 then
        year = year + 1;
        date = date - 365;
    elseif 730 < date and date <= 1096 then
        year = year + 2;
        date = date - 730;
        mon[2] = 29
    elseif 1096 < date then
        year = year + 3;
        date = date - 1096;
    end



    while (1) do
        if date > mon[month] then
            date = date - mon[month]
            month = month + 1
        else
            date = date + 1;
            break;
        end
    end

    return year, month, date, hour, minite, secend

end


return M